package org.javarosa.mesh4j.sync.samples;

import java.util.Date;
import java.util.Vector;

import org.javarosa.mesh4j.sync.adapter.DataModelTreeContentAdapter;
import org.javarosa.mesh4j.sync.adapter.UserContentAdapter;
import org.javarosa.user.model.User;
import org.mesh4j.sync.SyncEngine;
import org.mesh4j.sync.adapters.feed.XMLContent;
import org.mesh4j.sync.adapters.feed.rss.RssSyndicationFormat;
import org.mesh4j.sync.adapters.http.HttpSyncAdapter;
import org.mesh4j.sync.adapters.rms.storage.RmsStorageSyncRepository;
import org.mesh4j.sync.adapters.split.SplitAdapter;
import org.mesh4j.sync.id.generator.IdGenerator;
import org.mesh4j.sync.model.IContent;
import org.mesh4j.sync.model.Item;
import org.mesh4j.sync.model.Sync;
import org.mesh4j.sync.security.NullIdentityProvider;

public class Mesh4jExtensionSamples {

	public static final String USER_URL = "http://vcc-pc:8080/Mesh4j/userFeed";
	public static final String XFORM_URL = "http://vcc-pc:8080/Mesh4j/XForm";
	//public static final String XFORM_URL = "http://sync.instedd.org/Service.svc/feeds/JavaRosaDemo";
	//public static final String XFORM_URL = "http://vcc-pc:1495/Service.svc/feeds/XForm";
		
	public static final String USER = "Users";

	// USERS
	public static String synchronizeUsers(){

		RmsStorageSyncRepository syncRepo = new RmsStorageSyncRepository("syncInfoUsers", NullIdentityProvider.INSTANCE, IdGenerator.INSTANCE);
		UserContentAdapter contentAdapter = new UserContentAdapter(USER);
		
		SplitAdapter splitAdapter = new SplitAdapter(syncRepo, contentAdapter, NullIdentityProvider.INSTANCE);
		
		splitAdapter.add(makeNewUser());
		splitAdapter.add(makeNewUser());
		splitAdapter.add(makeNewUser());
		splitAdapter.add(makeNewUser());
		
		HttpSyncAdapter httpAdapter = new HttpSyncAdapter(USER_URL, RssSyndicationFormat.INSTANCE, NullIdentityProvider.INSTANCE, IdGenerator.INSTANCE);
		
		SyncEngine syncEngine = new SyncEngine(splitAdapter, httpAdapter);
		syncEngine.synchronize();

		String result = printLocalItems(splitAdapter);

//		contentAdapter.deleteRecordStorage();
//		syncRepo.deleteRecordStorage();
		
		return result;
	}

	private static String printLocalItems(SplitAdapter splitAdapter) {
		
		StringBuffer sb = new StringBuffer();
		Vector<Item> items = splitAdapter.getAll();
		
		sb.append("Local Items: ");
		sb.append(items.size());
		sb.append("\n");
		
		Item item = null;
		for (int i = 0; i < items.size(); i++) {
			item = items.elementAt(i);
			sb.append(item.getSyncId());
			sb.append(" ");
			sb.append(item.getContent().getPayload());
			sb.append("\n");
		}
		return sb.toString();
	}

	private static Item makeNewUser() {
		String id = "phone"+IdGenerator.INSTANCE.newID();
		String syncId = IdGenerator.INSTANCE.newID();
		
		User user = new User(id, "123", User.STANDARD);
		
		XMLContent content = new XMLContent(id, "user: " + id, "user from mobile", UserContentAdapter.formatUser(user));
		Sync sync = new Sync(syncId, "jmt", new Date(), false);
		return new Item(content, sync);
	}

	// FORMS
	
	public static String synchronizeXForms(){
				
		StringBuffer sb = new StringBuffer();
		
		DataModelTreeContentAdapter contentAdapter = new DataModelTreeContentAdapter("XFormDemo2");
		
		Vector<IContent> contents = contentAdapter.getAll(null);
		IContent content = null;
		for (int i = 0; i < contents.size(); i++) {
			content = contents.elementAt(i);
			sb.append("/n");
			sb.append(content.getId() + " " + content.getPayload());
		}
		return sb.toString();
	}
}
